/**
 * Created by hxpeng on 2017/4/18.
 */
(function () {
    'use strict';

    angular.module('App.pages.dynamic')
        .controller('DynamicCtrl', DynamicCtrl);

    function DynamicCtrl($scope,DynamicModel,$location,LoginAccountAuth,$timeout,AppUtils) {
        //逻辑代码
        var vm = this;
        vm.loginAccountVo = {};
        vm.exhortaions = [];
        vm.loadMoreBtnStatus = true;
        vm.pageIndex = 0;


        //效果事件
        //页面进入先绑定事件，（即使是缓存页面也会执行）
        $scope.$on('$ionicView.enter', function(){
            LoginAccountAuth.getAccount().then(function(data){
                vm.loginAccountVo = data;
                if(data.memberType === '1'){
                    $('a.target-burger').unbind("click").bind("click",function(e){
                        if(!$('nav.main-nav').hasClass('toggled')){
                            var timer1 = $timeout(
                                function() {
                                    $('#btn1').toggleClass('toggled');
                                },
                                0
                            );
                            var timer2 = $timeout(
                                function() {
                                    $('#btn2').toggleClass('toggled');
                                },
                                50
                            );
                            var timer3 = $timeout(
                                function() {
                                    $('#btn3').toggleClass('toggled');
                                },
                                100
                            );
                            $('nav.main-nav, a.target-burger').addClass('toggled');
                        }else{
                            var timer1 = $timeout(
                                function() {
                                    $('#btn3').removeClass('toggled');
                                    $('#btn2').removeClass('toggled');
                                    $('#btn1').removeClass('toggled');
                                },
                                100
                            );
                            timer1.then(
                                function() {
                                    $('nav.main-nav, a.target-burger').removeClass('toggled');
                                }
                            );
                        }

                        e.preventDefault();
                    })
                }
            })
        });

        $scope.$on('$ionicView.beforeLeave', function(){
            if($(".main-nav").hasClass('toggled')){
                $('a.target-burger').click();
            }
        });

        $scope.createNewDynamic = function(){
            $location.path('/dynamic/create');
        }


        $scope.loadMoreDynamic = function(){
            if(vm.loadMoreBtnStatus){
                vm.pageIndex = vm.pageIndex + 1;
                vm.getDynamic();
            }
        }

        vm.getDynamic = function(){
            var params = {};
            params.pageInfo = {};
            params.pageInfo.pageSize = 10;
            params.pageInfo.pageNumber = vm.pageIndex;

            DynamicModel.dynamicList(params).$promise.then(function(response){
                if(response.stat === "success"){
                    var reponseDate = response.pageResult;
                    var items = reponseDate.pageResult;
                    var pageIndex = reponseDate.pageIndex;
                    var pageTotalPageSize = reponseDate.pageTotalPageSize;
                    if(pageIndex == pageTotalPageSize || items.length == 0){
                        vm.loadMoreBtnStatus = false;
                    }
                    angular.forEach(items,function(data){
                        vm.exhortaions.push(data);
                    })
                }else{
                    AppUtils.showTipsTop("获取动态失败，请稍后再试！");
                    vm.loadMoreBtnStatus = false;
                }
            }).finally(function() {
                // Stop the ion-refresher from spinning
                $scope.$broadcast('scroll.infiniteScrollComplete');
            });
        }

    }

})();
